#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    pHighlighter = new RegexpHighlighter(ui->edRegExp->document());
    pDynHighlighter = new QDynamicHighlighter(ui->edSourceText->document());
    pParamDB = new QParamDB();

    UpdateCombobox();

    QObject::connect(ui->edRegExp, SIGNAL(textChanged()), this, SLOT(CheckRegexpValidity()));
//    ui->btnDoReplace->addAction(ui->aReplaceStart);
//    ui->btnLoadText->addAction(ui->aLoad);
    QObject::connect(ui->cbRegexpList, SIGNAL(currentIndexChanged(QString)), this, SLOT(LoadRegexpByName(QString)));
}

MainWindow::~MainWindow()
{
    delete ui;
    delete pHighlighter;
    delete pDynHighlighter;
    delete pParamDB;
}

void MainWindow::CheckRegexpValidity()
{
    regExp.setPattern(ui->edRegExp->toPlainText());
    if (regExp.isValid()){

        ui->lbValidity->setText(trUtf8("<span style=\" font-size:10pt; color:#00cc00;\">Доспустимый синтаксис</span>"));
        ChangePattern();
        ui->btnDoReplace->setEnabled(true);
        ui->edSourceText->setPlainText(ui->edSourceText->toPlainText());
    }
    else
    {
        ui->lbValidity->setText(trUtf8("<span style=\" font-size:10pt; color:#ff0000;\">Ошибка синтаксиса!</span>"));
        ui->btnDoReplace->setEnabled(false);
    }
}

void MainWindow::ChangePattern()
{
    QRegExp rx(ui->edRegExp->toPlainText());
    if (!rx.exactMatch("")){
       pDynHighlighter->SetPattern(ui->edRegExp->toPlainText());
    }

}

void MainWindow::LoadRegexpByName(const QString name)
{
    ui->edRegExp->setText(pParamDB->GetValueByName(name));

}

void MainWindow::TestSlot()
{
    ui->edRegExp->setText(pParamDB->GetValueByName("Name1"));
}

void MainWindow::on_aLoad_triggered()
{
    QString str = QFileDialog::getOpenFileName(0, "Loading text file", "", "Textfile *.txt *.html;;Sources (*.pas)");

    QFile aFile(str);
    if (aFile.open(QIODevice::ReadOnly)){
        ui->edSourceText->setPlainText(aFile.readAll());
    }
}

void MainWindow::on_aReplaceStart_triggered()
{
    QString str = ui->edSourceText->toPlainText();
    regExp.setPattern(ui->edRegExp->toPlainText());
    str.replace(regExp, ui->edReplacePattern->toPlainText());
    ui->edReplaceResult->setPlainText(str);
}

void MainWindow::on_aAddRegexp_triggered()
{
    QString rxName = QInputDialog::getText(this, trUtf8("Сохранить регулярное выражение"), trUtf8("Введите имя регулярного выражения"));
    pParamDB->InsertNewParam(rxName, ui->edRegExp->toPlainText());
    UpdateCombobox();
}

void MainWindow::on_aDeleteRegexp_triggered()
{
    pParamDB->RemoveParam(ui->cbRegexpList->currentText());
    UpdateCombobox();
}

void MainWindow::on_aCopyToClipboard_triggered()
{
    QString str = ui->edRegExp->toPlainText();
    QApplication::clipboard()->setText("\""+str.replace("\\","\\\\")+"\"");
}

void MainWindow::UpdateCombobox()
{
    QStringList sl = pParamDB->getNames().split(",");
    ui->cbRegexpList->clear();
    foreach(QString name, sl){
        ui->cbRegexpList->addItem(name);
    }
}
